package com.tequila.core.service.impl;

import com.google.common.collect.Lists;
import com.tequila.core.ErrorConstants;
import com.tequila.core.dao.UserDao;
import com.tequila.core.service.UserService;
import com.tequila.domain.dto.UserDto;
import com.tequila.domain.entity.User;
import com.tequila.domain.query.UserQuery;
import fe.core.Assert;
import fe.core.BeanHelper;
import fe.core.Pagination;
import fe.core.UserDetail;
import fe.core.springmvc.UserDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    public static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Resource
    private UserDao userDao;

    @Override
    public Pagination<UserDto> queryPageListByParam(UserQuery query) {
        Assert.assertNotNull(query);
        int total = userDao.queryPageCountByParam(query);

        List<UserDto> list = Lists.newArrayList();
        if (total > 0) {
            list = userDao.queryPageListByParam(query);
        }
        return new Pagination<>(query, list, total);
    }

    @Override
    public UserDto queryById(Long id) {
        Assert.assertNotNull(id,"id不能为空!");
        User user = userDao.queryById(id);
        UserDto userDto = new UserDto();
        BeanHelper.copyProperties(userDto,user);
        return userDto;
    }

    @Override
    public List<UserDto> queryByIdList(List<Long> idList) {
        Assert.assertNotNull(idList,"idList不能为空!");
        return userDao.queryByIdList(idList);
    }

    @Override
    public List<UserDto> queryListByParam(UserQuery query) {
        Assert.assertNotNull(query,"query不能为空!");
        return userDao.queryListByParam(query);
    }


    @Override
    public Long insert(User entity) {
        Assert.assertNotNull(entity,"entity不能为空!");
        userDao.insert(entity);
        return entity.getId();
    }

    @Override
    public Long save(User entity) {
        Assert.assertNotNull(entity,"entity不能为空!");
        userDao.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public int updateById(User param) {
        Assert.assertNotNull(param,"param不能为空!");
        Assert.assertNotNull(param.getId(),"id不能为空!");
        return userDao.updateById(param);
    }

    @Override
    public int updateSelectiveById(User param) {
        Assert.assertNotNull(param,"param不能为空!");
        Assert.assertNotNull(param.getId(),"id不能为空!");
        return userDao.updateSelectiveById(param);
    }

    @Override
    public int deleteById(Long id) {
        Assert.assertNotNull(id,"id不能为空!");
        return userDao.deleteById(id);
    }

    @Override
    public int deleteByIdList(List<Long> idList) {
        Assert.assertNotNull(idList,"idList不能为空!");
        return userDao.deleteByIdList(idList);
    }


    @Override
    public int insertOrUpdate(UserDto userDto) {

        Assert.assertNotNull(userDto, ErrorConstants.ID_ERR);
        if (userDto.getId() != null) {
            User user = new User();
            BeanUtils.copyProperties(userDto, user);
            return userDao.updateSelectiveById(user);
        }

        User user = userDao.selectByOpenid(userDto.getWeChatOpenId());
        if (user == null) {
            user = new User();
            BeanUtils.copyProperties(userDto, user);
            int i = userDao.insertSelective(user);
            userDto.setId(user.getId());

            return i;
        } else {
            userDto.setId(user.getId());
            BeanUtils.copyProperties(userDto, user);
            return userDao.updateSelectiveById(user);
        }
    }

}
